package com.example.enterprisemanager.mvvm.main;

import android.app.Application;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.example.enterprisemanager.base.BaseViewModel;
import com.example.enterprisemanager.mvvm.ben.User;

import cn.bmob.v3.BmobUser;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class MainViewModel extends BaseViewModel<MainModel> {

    public MutableLiveData<User> sendUserData;

    public MainViewModel(@NonNull Application application) {
        super(application, new MainModel());
        init();
    }

    private void init() {
        sendUserData = new MutableLiveData<>();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mModel.isLogin(new Consumer<User>() {
            @Override
            public void accept(User user) throws Exception {
                sendUserData.postValue(user);
            }
        }, new Action() {
            @Override
            public void run() throws Exception {
                sendUserData.postValue(null);
            }
        });

    }

    public void loginOut() {
        BmobUser.logOut();
        sendUserData.postValue(null);
    }
}
